<<<<<<< HEAD
package com.wyz.controller;

import cn.hutool.core.lang.UUID;
import cn.hutool.json.JSONObject;
import com.wyz.common.AutoLog;
import com.wyz.common.ResultVo;
import com.wyz.dto.UserInfoDto;
import com.wyz.dto.UserLoginDto;
import com.wyz.entity.UserEntity;
import com.wyz.service.UserService;
import com.wyz.util.HutoolCaptchaUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wk
 * @desc
 * @date 2024/12/25 16:06
 */
@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    //注册用户
    @AutoLog("用户注册")

    @PostMapping("/register")
    public ResultVo<Boolean> register(@RequestBody UserEntity userEntity){
        log.info("用户注册请求:{}",userEntity);
        boolean result = userService.register(userEntity);
        log.info("用户注册结果:{}",result);
        if(result){
            return new ResultVo<>(200, "注册成功", true);
        }
        return new ResultVo<>(500, "注册失败", false);
    }
    //登录用户
    @AutoLog("用户登录")
    /**
     * 验证码生成器
     * @return 验证码图片
     * @throws IOException
     */
    @GetMapping("/getCaptcha")
    public Map<String,String> getCaptcha() throws IOException {
        System.out.println("获取验证码图片请求");

        // 生成验证码图片
        String uuid = UUID.randomUUID().toString();
        System.out.println(uuid);
        String base64Image = HutoolCaptchaUtil.generateCaptcha(uuid);

        // 封装JSON数据
        Map<String,String> resultMap = new HashMap<>();
        //将uuid和验证码图片base64编码存入map
        resultMap.put("uuid", uuid);
        //将验证码图片base64编码存入map
        resultMap.put("captcha", base64Image);

        return resultMap;
    }


    /**
     * 验证验证码
     * @param userDTO  用户信息
     * @return
     */

    /**
     * 用户登录
     * @return    返回登录成功信息
     */
    @AutoLog("用户登录")
    @PostMapping("/login")
    public ResultVo<Boolean> login(@RequestBody UserLoginDto userLoginDto) {
        log.info("用户请求参数: {}", userLoginDto);
        String userAccount = userLoginDto.getUserAccount();
        String userPassword = userLoginDto.getUserPassword();
        //验证码验证
        String realCaptcha = HutoolCaptchaUtil.getCheckCode(userLoginDto.getUuid());
        String captcha = userLoginDto.getCheckCode();
        if (captcha != null && captcha.equals(realCaptcha)) {
            // 验证码正确，删除验证码
            HutoolCaptchaUtil.removeCheckCode(userLoginDto.getUuid());}
        //参数校验检查字段是否为空
        if (StringUtils.isAnyBlank(userAccount, userPassword, captcha)) {
            return ResultVo.fail();
        }
        try {
            boolean result = userService.login(userAccount, userPassword);
            if (result){
                return new ResultVo<>(200, "登录成功", true);
            }else{
                return new ResultVo<>(500, "登录失败", false);
            }
        } catch (IllegalArgumentException e) {
            // 捕获Service层的异常，返回用户错误信息
            log.error("登录失败: {}", e.getMessage());
            return ResultVo.fail();
        } catch (Exception e) {
            // 捕获其他异常
            log.error("系统错误: {}", e.getMessage());
            return ResultVo.fail();
        }
    }
    // 处理用户忘记密码请求，通过邮箱发送新密码
    //通过用户名查找用户，然后发送重置密码的邮件
    @AutoLog("用户忘记密码")
    @PostMapping("/forgotPassword/{userAccount}")
    public ResultVo<Boolean> forgotPassword(@PathVariable("userAccount") String userAccount) {
        boolean success = userService.sendResetPasswordEmail(userAccount);
        if (success) {
            return new ResultVo<>(200, "重置密码的邮件已发送，请查收。", true);
        } else {
            return new ResultVo<>(500, "该邮箱未注册，请检查邮箱地址是否正确。", false);

        }
    }








    //用户更新信息
    //通过用户id更新用户信息
    @AutoLog("用户更新信息")
    @PostMapping("/updateUserInformation/{UserId}")
    public ResultVo<Boolean> updateUserInformation(@PathVariable("UserId") Integer UserId, @RequestBody UserInfoDto userInfoDto) {
        log.info("更新用户信息: {}", userInfoDto);
        if (userInfoDto == null) {
            return ResultVo.fail();
        }
        try {
            //更新用户信息
            boolean result = userService.updateUserInfo(UserId, userInfoDto);
            return result ? ResultVo.success() : ResultVo.fail();
        } catch (IllegalArgumentException e) {
            // 捕获Service层的异常，返回用户友好的错误信息
            log.error("更新失败: {}", e.getMessage());
            return ResultVo.fail();
        } catch (Exception e) {
            // 捕获其他异常
            log.error("系统错误: {}", e.getMessage());
            return ResultVo.fail();
        }
    }
    @GetMapping("/selectUserByAccount/{account}")
    public ResultVo<UserEntity> selectUserByAccount(@PathVariable("account") String account) {
        log.info("根据用户账号查询用户信息: {}", account);
        UserEntity userEntity = userService.selectUserByAccount(account);
        if (userEntity == null) {
            return ResultVo.fail();
        }
        return new ResultVo(200, "查询成功",userEntity);
    }
}
=======
package com.wyz.controller;

import cn.hutool.core.lang.UUID;
import cn.hutool.json.JSONObject;
import com.wyz.common.AutoLog;
import com.wyz.common.ResultVo;
import com.wyz.dto.UserInfoDto;
import com.wyz.dto.UserLoginDto;
import com.wyz.entity.UserEntity;
import com.wyz.service.UserService;
import com.wyz.util.HutoolCaptchaUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wk
 * @desc
 * @date 2024/12/25 16:06
 */
@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    //注册用户
    @AutoLog("用户注册")

    @PostMapping("/register")
    public ResultVo<Boolean> register(@RequestBody UserEntity userEntity){
        log.info("用户注册请求:{}",userEntity);
        boolean result = userService.register(userEntity);
        log.info("用户注册结果:{}",result);
        if(result){
            return new ResultVo<>(200, "注册成功", true);
        }
        return new ResultVo<>(500, "注册失败", false);
    }
    //登录用户
    @AutoLog("用户登录")
    /**
     * 验证码生成器
     * @return 验证码图片
     * @throws IOException
     */
    @GetMapping("/getCaptcha")
    public Map<String,String> getCaptcha() throws IOException {
        System.out.println("获取验证码图片请求");

        // 生成验证码图片
        String uuid = UUID.randomUUID().toString();
        System.out.println(uuid);
        String base64Image = HutoolCaptchaUtil.generateCaptcha(uuid);

        // 封装JSON数据
        Map<String,String> resultMap = new HashMap<>();
        //将uuid和验证码图片base64编码存入map
        resultMap.put("uuid", uuid);
        //将验证码图片base64编码存入map
        resultMap.put("captcha", base64Image);

        return resultMap;
    }


    /**
     * 验证验证码
     * @param userDTO  用户信息
     * @return
     */

    /**
     * 用户登录
     * @return    返回登录成功信息
     */
    @AutoLog("用户登录")
    @PostMapping("/login")
    public ResultVo<Boolean> login(@RequestBody UserLoginDto userLoginDto) {
        log.info("用户请求参数: {}", userLoginDto);
        String userAccount = userLoginDto.getUserAccount();
        String userPassword = userLoginDto.getUserPassword();
        //验证码验证
        String realCaptcha = HutoolCaptchaUtil.getCheckCode(userLoginDto.getUuid());
        String captcha = userLoginDto.getCheckCode();
        if (captcha != null && captcha.equals(realCaptcha)) {
            // 验证码正确，删除验证码
            HutoolCaptchaUtil.removeCheckCode(userLoginDto.getUuid());}
        //参数校验检查字段是否为空
        if (StringUtils.isAnyBlank(userAccount, userPassword, captcha)) {
            return ResultVo.fail();
        }
        try {
            boolean result = userService.login(userAccount, userPassword);
            if (result){
                return new ResultVo<>(200, "登录成功", true);
            }else{
                return new ResultVo<>(500, "登录失败", false);
            }
        } catch (IllegalArgumentException e) {
            // 捕获Service层的异常，返回用户错误信息
            log.error("登录失败: {}", e.getMessage());
            return ResultVo.fail();
        } catch (Exception e) {
            // 捕获其他异常
            log.error("系统错误: {}", e.getMessage());
            return ResultVo.fail();
        }
    }
    // 处理用户忘记密码请求，通过邮箱发送新密码
    //通过用户名查找用户，然后发送重置密码的邮件
    @AutoLog("用户忘记密码")
    @PostMapping("/forgotPassword/{userAccount}")
    public ResultVo<Boolean> forgotPassword(@PathVariable("userAccount") String userAccount) {
        boolean success = userService.sendResetPasswordEmail(userAccount);
        if (success) {
            return new ResultVo<>(200, "重置密码的邮件已发送，请查收。", true);
        } else {
            return new ResultVo<>(500, "该邮箱未注册，请检查邮箱地址是否正确。", false);

        }
    }








    //用户更新信息
    //通过用户id更新用户信息
    @AutoLog("用户更新信息")
    @PostMapping("/updateUserInformation/{UserId}")
    public ResultVo<Boolean> updateUserInformation(@PathVariable("UserId") Integer UserId, @RequestBody UserInfoDto userInfoDto) {
        log.info("更新用户信息: {}", userInfoDto);
        if (userInfoDto == null) {
            return ResultVo.fail();
        }
        try {
            //更新用户信息
            boolean result = userService.updateUserInfo(UserId, userInfoDto);
            return result ? ResultVo.success() : ResultVo.fail();
        } catch (IllegalArgumentException e) {
            // 捕获Service层的异常，返回用户友好的错误信息
            log.error("更新失败: {}", e.getMessage());
            return ResultVo.fail();
        } catch (Exception e) {
            // 捕获其他异常
            log.error("系统错误: {}", e.getMessage());
            return ResultVo.fail();
        }
    }
    @GetMapping("/selectUserByAccount/{account}")
    public ResultVo<UserEntity> selectUserByAccount(@PathVariable("account") String account) {
        log.info("根据用户账号查询用户信息: {}", account);
        UserEntity userEntity = userService.selectUserByAccount(account);
        if (userEntity == null) {
            return ResultVo.fail();
        }
        return new ResultVo(200, "查询成功",userEntity);
    }
}
>>>>>>> be516e15a35375b38cf973b1ce88c92dd1ceba03
